home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / anydbm.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  3KB  |  89 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. """Generic interface to all dbm clones.
  5.  
  6. Instead of
  7.  
  8.         import dbm
  9.         d = dbm.open(file, 'w', 0666)
  10.  
  11. use
  12.  
  13.         import anydbm
  14.         d = anydbm.open(file, 'w')
  15.  
  16. The returned object is a dbhash, gdbm, dbm or dumbdbm object,
  17. dependent on the type of database being opened (determined by whichdb
  18. module) in the case of an existing dbm. If the dbm does not exist and
  19. the create or new flag ('c' or 'n') was specified, the dbm type will
  20. be determined by the availability of the modules (tested in the above
  21. order).
  22.  
  23. It has the following interface (key and data are strings):
  24.  
  25.         d[key] = data   # store data at key (may override data at
  26.                         # existing key)
  27.         data = d[key]   # retrieve data at key (raise KeyError if no
  28.                         # such key)
  29.         del d[key]      # delete data stored at key (raises KeyError
  30.                         # if no such key)
  31.         flag = key in d   # true if the key exists
  32.         list = d.keys() # return a list of all existing keys (slow!)
  33.  
  34. Future versions may change the order in which implementations are
  35. tested for existence, and add interfaces to other dbm-like
  36. implementations.
  37. """
  38.  
  39. class error(Exception):
  40.     pass
  41.  
  42. _names = [
  43.     'dbhash',
  44.     'gdbm',
  45.     'dbm',
  46.     'dumbdbm']
  47. _errors = [
  48.     error]
  49. _defaultmod = None
  50. for _name in _names:
  51.     
  52.     try:
  53.         _mod = __import__(_name)
  54.     except ImportError:
  55.         continue
  56.  
  57.     if not _defaultmod:
  58.         _defaultmod = _mod
  59.     _errors.append(_mod.error)
  60.  
  61. if not _defaultmod:
  62.     raise ImportError, 'no dbm clone found; tried %s' % _names
  63. error = tuple(_errors)
  64.  
  65. def open(file, flag = 'r', mode = 438):
  66.     """Open or create database at path given by *file*.
  67.  
  68.     Optional argument *flag* can be 'r' (default) for read-only access, 'w'
  69.     for read-write access of an existing database, 'c' for read-write access
  70.     to a new or existing database, and 'n' for read-write access to a new
  71.     database.
  72.  
  73.     Note: 'r' and 'w' fail if the database doesn't exist; 'c' creates it
  74.     only if it doesn't exist; and 'n' always creates a new database.
  75.     """
  76.     whichdb = whichdb
  77.     import whichdb
  78.     result = whichdb(file)
  79.     if result is None:
  80.         if 'c' in flag or 'n' in flag:
  81.             mod = _defaultmod
  82.         else:
  83.             raise error, "need 'c' or 'n' flag to open new db"
  84.     if result == '':
  85.         raise error, 'db type could not be determined'
  86.     mod = __import__(result)
  87.     return mod.open(file, flag, mode)
  88.  
  89.